//
// Created by huati on 2019/11/12.
//
#include "jni.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <android/log.h>

#define LOGD(FORMAT, ...) __android_log_print(ANDROID_LOG_DEBUG,"zp",FORMAT,##__VA_ARGS__);


char password[] = "imbeijingtimezero";
//加密
extern "C" JNIEXPORT void JNICALL
Java_com_bjtime_designer_Jutils_nativeSaveHpiFile(JNIEnv *env, jclass jcla,
                                                  jstring normal_path_jstr,
                                                  jstring crypt_path_jstr) {
//jstring --> char*
    const char *normal_path = env->GetStringUTFChars(normal_path_jstr, NULL);
    const char *crypt_path = env->GetStringUTFChars(crypt_path_jstr, NULL);

    LOGD("%s", normal_path);
//打开文件
    FILE *nomal_fp = fopen(normal_path, "rb");
    FILE *crypt_fp = fopen(crypt_path, "wb");

    if (nomal_fp == NULL) {
        LOGD("%s", "文件打开失败");
        return;
    }

//一次读取一个字符
    int ch = 0;
    int i = 0;
    int pwd_length = strlen(password);
    while ((ch = fgetc(nomal_fp)) != EOF) {    //End of File
//写入(异或运算)
        fputc(ch ^ password[i % pwd_length], crypt_fp);
        i++;
    }

//关流
    fclose(nomal_fp);
    fclose(crypt_fp);
}




//解密
extern "C" JNIEXPORT void JNICALL
Java_com_bjtime_designer_Jutils_nativeOpenHpiFile(JNIEnv *env, jclass jcla, jstring crypt_path_jstr,
                                                  jstring decrypt_path_jstr) {
//jstring --> char*
    const char *crypt_path = env->GetStringUTFChars(crypt_path_jstr, NULL);
    const char *decrypt_path = env->GetStringUTFChars(decrypt_path_jstr, NULL);
    FILE *crypt_fp = fopen(crypt_path, "rb");
    FILE *decrypt_fp = fopen(decrypt_path, "wb");

    int ch;
    int i = 0;
    int pwd_length = strlen(password);
    while ((ch = fgetc(crypt_fp)) != EOF) {
        fputc(ch ^ password[i % pwd_length], decrypt_fp);
        i++;
    }

    fclose(crypt_fp);
    fclose(decrypt_fp);
}